$(function () {
	vm.category();
	vm.city();
    $("#jqGrid").jqGrid({
        url: baseURL + 'tinfo/listAll',
        datatype: "json",
        colModel: [			
			{ label: '景点名称', name: 'tourName', index: 'tour_name', width: 80 }, 			
			{ label: '景点信息', name: 'tourInfo', index: 'tour_info', width: 80 }, 			
			{ label: '景点图片存储地址', name: 'tourImage', index: 'tour_image', width: 80 }, 			
			{ label: '所属类别', name: 'category', index: 't_category_id', width: 80 }, 			
			{ label: '景区地址', name: 'tourAddr', index: 'tour_addr', width: 80 }, 			
			{ label: '旅游时间', name: 'tourTime', index: 'tour_time', width: 80 }, 			
			{ label: '开放时间', name: 'tourOpenTime', index: 'tour_open_time', width: 80 }, 			
			{ label: '门票', name: 'tourTicket', index: 'tour_ticket', width: 80 }, 			
			{ label: '交通路线', name: 'tourJiaotong', index: 'tour_jiaotong', width: 80 }, 			
			{ label: '旅游提示', name: 'tourTishi', index: 'tour_tishi', width: 80 }, 			
			{ label: '城市分类', name: 'city', index: 't_city_id', width: 80 }, 			
							
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: false, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tInfo: {},
		categoryEntity:[],
		citys:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.tInfo.id == null ? "tinfo/save" : "tinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.tInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){

							var location = $("input[name='image']").val();
							
							if (location==null||location=="") {}
							else {
								$
										.ajaxFileUpload({
											async : false,
											url : baseURL+"tinfo/tInfoImage/"+r.tInfo.id, // 用于文件上传的服务器端请求地址
											secureuri : false, // 是否需要安全协议，一般设置为false
											fileElementId : 'image', // 文件上传域的ID
											dataType : 'json', // 返回值类型
																// 一般设置为json
											success : function(
													data,
													status) // 服务器成功响应处理函数
											{
												vm.reload();
											},
											error : function(
													data,
													status,
													e)// 服务器响应失败处理函数
											{

											}
										})
							
								
							}
							
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "tinfo/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "tinfo/info/"+id, function(r){
                vm.tInfo = r.tInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		category:function(){
			$.ajax({
				type : "POST",
				url : baseURL+"tcategory/listInfoAll",
				dataType : "json",
				success : function(r) {
					if (r.code === 0) {
						vm.categoryEntity=r.categoryEntity;

					} else {
						alert(r.msg);
					}
				}
			});
		},
		city:function(){
			$.ajax({
				type : "POST",
				url : baseURL+"tcity/listInfoAll",
				dataType : "json",
				success : function(r) {
					if (r.code === 0) {
						vm.citys=r.tCityEntity;
					} else {
						alert(r.msg);
					}
				}
			});
		}
	}
});